package com.i2bt.app.share;

import com.i2bt.common.AndroidRestContract;

import com.i2bt.R;
import com.i2bt.adapter.SharedFriendListAdapter;
import com.i2bt.adapter.SharedMyListAdapter;
import com.i2bt.app.BaseFragment;
import com.i2bt.app.main.MainActivity;
import com.i2bt.app.share.model.FriendShareJsonHandle;
import com.i2bt.app.share.model.GetMyShareJsonHandle;
import com.i2bt.app.share.model.GetShareJsonHandle;
import com.i2bt.app.share.model.MyShareJsonHandle;
import com.i2bt.bean.GetSharedBean;
import com.i2bt.bean.ShareBean;
import com.i2bt.util.VisibilityUtil;
import com.i2bt.view.XListView;
import com.i2bt.view.XListView.IXListViewListener;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;

public class ShareFragment extends BaseFragment {

	public XListView mySharedListView;

	public List<GetSharedBean> mySharedFilesList;

	public static SharedMyListAdapter mySharedListAdapter;

	public Button SharedBtn;

	public List<GetSharedBean> friendSharedFilesList;

	public XListView friendSharedListView;

	public SharedFriendListAdapter friendSharedListAdapter;

	public ShareFragment() {
		mySharedFilesList = new ArrayList<GetSharedBean>();
		friendSharedFilesList = new ArrayList<GetSharedBean>();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_shared, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		initView();
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public void onDestroyOptionsMenu() {
		// TODO Auto-generated method stub
		super.onDestroyOptionsMenu();
		this.hideSharedBtn();
	}

	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		if (isVisibleToUser) {
			if (mySharedFilesList.size() < 1
					&& (mySharedListView.getVisibility() == View.VISIBLE)) {
				refreshMyShare();
				System.out.println("刷新我的分享");
			} else if (friendSharedFilesList.size() < 1
					&& (friendSharedListView.getVisibility() == View.VISIBLE)) {
				refreshFriendShare();
				System.out.println("刷新好友分享");
			}
			this.showSharedBtn();
		}
		super.setUserVisibleHint(isVisibleToUser);
	}

	@Override
	protected void initView() {
		mySharedListAdapter = new SharedMyListAdapter(this, mySharedFilesList);
		mySharedListView = (XListView) activity
				.findViewById(R.id.shared_myshared_list);
		mySharedListView.setPullLoadEnable(false);
		mySharedListView.setAdapter(mySharedListAdapter);
		mySharedListView.setXListViewListener(new IXListViewListener() {
			@Override
			public void onRefresh() {
				refreshMyShare();
			}

			@Override
			public void onLoadMore() {

			}
		});
		friendSharedListAdapter = new SharedFriendListAdapter(this,
				friendSharedFilesList);
		friendSharedListView = (XListView) activity
				.findViewById(R.id.shared_friendshared_list);
		friendSharedListView.setPullLoadEnable(false);
		friendSharedListView.setAdapter(friendSharedListAdapter);
		friendSharedListView.setXListViewListener(new IXListViewListener() {
			@Override
			public void onRefresh() {
				refreshFriendShare();
			}

			@Override
			public void onLoadMore() {

			}
		});
		SharedBtn = (Button) activity.findViewById(R.id.shared_button);
		SharedBtn.setOnClickListener(this);
		mySharedListView.setOnItemClickListener(this);
		friendSharedListView.setOnItemClickListener(this);
	}

	public void showMyShare() {
		VisibilityUtil.setVisibilityUI(mySharedListView, true);
		VisibilityUtil.setVisibilityUI(friendSharedListView, false);
		refreshMyShare();
	}

	public void showFriendShare() {
		VisibilityUtil.setVisibilityUI(mySharedListView, false);
		VisibilityUtil.setVisibilityUI(friendSharedListView, true);
		refreshFriendShare();
	}

	public void showSharedBtn() {
		SharedBtn.setVisibility(View.VISIBLE);
	}

	public void hideSharedBtn() {
		SharedBtn.setVisibility(View.GONE);
	}

	@Override
	protected void logic() {

	}

	public void refreshMyShare() {
		new GetMyShareJsonHandle(this).postTo(
				AndroidRestContract.REST_GET_MYSHARE, null);
	}

	public void refreshFriendShare() {
		new GetShareJsonHandle(this).postTo(
				AndroidRestContract.REST_GET_FRIENDSHARE, null);
	}

	public void deleteMyShare(int i) {
		ShareBean b = new ShareBean();
		b.setAct(AndroidRestContract.CANCEL_MULT_SHARE);
		b.setPosition(i);
		System.out.println("删除前的" + i);
		b.setPath(mySharedFilesList.get(i).getHomeBoxPath());
		new MyShareJsonHandle(this).postTo(
				AndroidRestContract.REST_DELETE_MY_SHARE, b);
	}

	public void downloadMyShare(int i) {
		System.out.println("下载：" + i);
		ShareBean b = new ShareBean();
		b.setAct(AndroidRestContract.DOWNLOAD_FILES);
		b.setPath(mySharedFilesList.get(i).getHomeBoxPath());
		new FriendShareJsonHandle(this).postTo(AndroidRestContract.REST_SHARED,
				b);
		this.toastHandler("正在下载！");
		MainActivity act = (MainActivity) this.activity;
		act.transmission.addDownload(R.drawable.files_icon_image,
				mySharedFilesList.get(i).getFileName(), "1M/10M", "/sdcard");
	}

	public void modifyMyShare(int i) {
		Bundle bundle = new Bundle();
		bundle.putString("path", mySharedFilesList.get(i).getHomeBoxPath());
		this.goActivity(ShareChangeShareActivity.class, bundle);
		System.out.println("修改" + i);
	}

	public void deleteFriendShare(int i) {
		ShareBean b = new ShareBean();
		b.setAct(AndroidRestContract.DELETE_SHERE);
		b.setPosition(i);
		System.out.println("删除前的" + i);
		b.setPath(friendSharedFilesList.get(i).getHomeBoxPath());
		new FriendShareJsonHandle(this).postTo(
				AndroidRestContract.REST_DELETE_FRIEND_SHARE, b);

	}

	public void downloadFriendShare(int i) {
		System.out.println("下载：" + i);
		ShareBean b = new ShareBean();
		b.setAct(AndroidRestContract.DOWNLOAD_FILES);
		b.setPath(friendSharedFilesList.get(i).getHomeBoxPath());
		new FriendShareJsonHandle(this).postTo(AndroidRestContract.REST_SHARED,
				b);
		this.toastHandler("正在下载！");
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.shared_button: {
			if (SharedBtn.getText().toString().equals("我的动态")) {
				SharedBtn.setText("收到分享");
				friendSharedListView.stopRefresh();
				showMyShare();
			} else {
				SharedBtn.setText("我的动态");
				mySharedListView.stopRefresh();
				showFriendShare();
			}
			break;
		}
		}
	}

	@Override
	public void backHomeOperate() {

	}

	@Override
	public boolean onKeyBack() {
		return false;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		if (arg0 == mySharedListView) {
			System.out.println("mySharedListView");
		} else if (arg1 == friendSharedListView) {
			System.out.println("friendSharedListView");
		}
		System.out.println(arg1.getClass());
		System.out.println(arg2);
		System.out.println(arg3);
	}

}
